
package forname;

/**
 * 使用 Class.forName() and newInstance() 的方式, 來產生一個 instance<br>
 * <br>
 * Copyright: Copyright (c) 2001-2007<br>
 * Company: 一零四資訊科技股份有限公司
 * 
 * @author Eric.Kao(eric.kao@104.com.tw)
 * @version 1.0
 */
public class ClassForNameTest
{
	public static void main( String[] args )
	{
		try
		{
			new ClassForNameTest();
		}
		catch( ClassNotFoundException ex )
		{
			ex.printStackTrace();
		}
		catch( InstantiationException ex )
		{
			ex.printStackTrace();
		}
		catch( IllegalAccessException ex )
		{
			ex.printStackTrace();
		}

	}

	public ClassForNameTest() throws ClassNotFoundException, IllegalAccessException, InstantiationException
	{
		String className = "forname.SimpleUserDataManager";

		Class kClass = Class.forName( className );

		// 比較這個 SimpleUserDataManager 是否為 UserDataManager sub class
		// 可將 SimpleUserDataManger implemnet 的 code 拿掉再來測試, 將可印出 "does not implement required..."
		Class interfaceClass = UserDataManager.class;
		if( !interfaceClass.isAssignableFrom( kClass ) )
		{
			System.out.println( className + " does not implement required interface " + interfaceClass.getName() + "." );
		}

		// 取得 SimpleUserDataManager object
		UserDataManager manager = ( UserDataManager )kClass.newInstance();

		System.out.println( "address:" + manager.getAddress() );
	}
}
